package cloud.ben.smallscalepigfarmmanagementsystem.controller.sow;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.sow.SowMatingInformation;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.sow.SowMatingRecords;
import cloud.ben.smallscalepigfarmmanagementsystem.service.sow.ISowMatingInformationService;
import cloud.ben.smallscalepigfarmmanagementsystem.service.sow.ISowMatingRecordsService;
import cloud.ben.smallscalepigfarmmanagementsystem.utils.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2022/4/10 23:26
 */
@RestController
@Api(value = "母猪配种记录信息管理", tags = "母猪配种记录信息管理")
public class SowMatingRecordsController {

    private static final Logger logger = LoggerFactory.getLogger(SowMatingRecordsController.class);

    @Autowired
    private ISowMatingRecordsService iSowMatingRecordsService;

    @PostMapping("/admin/sowMatingRecords")
    @ApiOperation(value = "增加母猪配种记录信息接口",notes = "",httpMethod = "POST")
    public RespBean insertSowMatingRecords(@Valid SowMatingRecords sowMatingRecords, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("增加失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        return iSowMatingRecordsService.insertSowMatingRecords(sowMatingRecords);
    }

    @DeleteMapping("/admin/sowMatingRecords/{id}")
    @ApiOperation(value = "根据id删除母猪配种记录信息接口",notes = "",httpMethod = "DELETE")
    public RespBean deleteSowMatingRecords(@PathVariable("id") String id){
        if (id == null) {
            logger.error("您输入的数据为空，请重新输入");
            return RespBean.error("您输入的数据为空，请重新输入");
        }
        Integer returnId = null;
        try {
            returnId = Integer.parseInt(id);
        } catch (Exception exception) {
            logger.error("您传入的id不合法，请重新输入");
            return RespBean.error("您传入的id不合法，请重新输入");
        }
        return iSowMatingRecordsService.deleteSowMatingRecords(returnId);
    }

    @PutMapping("/admin/sowMatingRecords")
    @ApiOperation(value = "修改母猪配种记录信息接口",notes = "",httpMethod = "PUT")
    public RespBean updateSowMatingRecords(@Valid SowMatingRecords sowMatingRecords,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("修改失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        return iSowMatingRecordsService.updateSowMatingRecords(sowMatingRecords);
    }

    @GetMapping("/admin/sowMatingRecords")
    @ApiOperation(value = "分页查询母猪配种记录信息接口",notes = "",httpMethod = "GET")
    public RespBean selectSowMatingRecordsListByPage(@RequestParam Integer pageNumber, @RequestParam Integer pageSize){
        return iSowMatingRecordsService.selectSowMatingRecordsByPage(pageNumber, pageSize);
    }
    @GetMapping("/admin/sowMatingRecordsNum")
    @ApiOperation(value = "查询母猪母猪配种记录信息数量接口",notes = "", httpMethod = "GET")
    public RespBean selectSowMatingRecordsNum(){
        return iSowMatingRecordsService.selectSowMatingRecordsNum();
    }
}
